package com.test2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ErrorCard {
	
	public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
        String path = "C:/Users/domains90/Desktop/";
        String name = "error2.txt";
        File file = new File(path+name);       
        try(BufferedReader br = new BufferedReader(new InputStreamReader(
        new FileInputStream(file), "UTF-8"));){
            String lineTxt = null;
            List<CardError> list = new LinkedList<>();
            while ((lineTxt = br.readLine()) != null) {  

            	String[] info = lineTxt.split("/");
            	
            	String unionid = info[0];

            	int wxcardNumber = Integer.parseInt(info[1]);
            	String pack_id = info[2];
            	JSONArray pack_desc = JSONArray.parseArray(info[3]);
            	String title = info[4];
            	int desc_number = 0;
            	for(Object obj : pack_desc) {
            		JSONObject json =  JSONObject.parseObject(JSON.toJSONString(obj));
            		int number = json.getIntValue("count");
            		desc_number = desc_number+number;
            	}
            	
      
            	
            	if(wxcardNumber > desc_number) {
            		CardError error = new CardError();
            		error.setUnionid(unionid);
            		error.setPackid(pack_id);
            		error.setTitle(title);
            		error.setCont(info[3]);
            		String desc =  String.format("券包卡券数量：%d,实际领取数量：%d", desc_number,wxcardNumber);
            		error.setDesc(desc);
            		error.setWxcarNumber(wxcardNumber);
            		error.setNumber(desc_number);
            		list.add(error);
            	}
            	System.out.println("unionid:"+unionid);
            	System.err.println("wxcardNumber:"+wxcardNumber);
              	System.err.println("desc_number:"+desc_number);
            	System.out.println("pack_id:"+pack_id);
            	System.out.println("title:"+title);
            	System.out.println("pack_desc:"+pack_desc);
            	System.out.println("------------------------------");
            } 
            String excel = "C:/Users/domains90/Desktop/调整表.xlsx";
            
           EasyExcel.write(excel, CardError.class).sheet("明细").doWrite(list);
        }
	}

}
